/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月29日
 */
package com.massyframework.beanskin.runtime.service.metadata;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.ModuleEvent;
import com.massyframework.beanskin.ModuleCreatedEvent;
import com.massyframework.beanskin.ModuleListener;
import com.massyframework.beanskin.ModuleRuntimeException;
import com.massyframework.beanskin.ModuleDestoryingEvent;
import com.massyframework.beanskin.util.IOUtils;
import com.massyframework.beanskin.util.exceptioncollect.ExceptionCollectUtils;

/**
 * 缺省的元数据仓储
 * 
 * <p>
 * 监听模块卸载事件，支持自动清除卸载模块的类型
 * 
 * @author huangkh
 *
 */
public final class DefaultMetadataRepository extends AbstractMetadataRepository 
	implements ModuleListener{

	private static final String PATH = "BEANSKIN-INF/config/metadata.config";
	private Logger logger = LoggerFactory.getLogger(DefaultMetadataRepository.class);
	/**
	 * 
	 */
	public DefaultMetadataRepository() {

	}

	@Override
	public void onEvent(ModuleEvent event) {
		if (event instanceof ModuleCreatedEvent) {
			//加载模块定义的类
			Module<?> module = event.getModule();
			this.scanning(module);
			return;
		}
		
		if (event instanceof ModuleDestoryingEvent) {
			Module<?> module = event.getModule();
			this.clear(module);
		}
	}
	
	protected void scanning(Module<?> module) {
		ClassLoader classLoader = module.getModuleClassLoader();
		URL url = classLoader.getResource(PATH);
		if (url != null) {
			InputStream is = null;
			try {
				is = url.openStream();
				BufferedReader bufferedReader = 
						new BufferedReader(
								new InputStreamReader(is));
				
				String line = null;
				while((line = bufferedReader.readLine()) != null)
				{
					line = line.trim();
					if (line.length() == 0) {
						continue;
					}
					
					if (line.startsWith("#")) {
						continue;
					}
					
					try {
						Class<?> clazz = classLoader.loadClass(line);
						this.getClassMetadata(clazz);
					}catch(Exception e) {
						if (logger.isErrorEnabled()) {
							logger.error("cannot load class[" + line + "] from " + module + ".", e);
						}
						
						ExceptionCollectUtils.addException(
								new ModuleRuntimeException(module, e));
					}
				}

			}catch(IOException e) {
				
			}finally {
				IOUtils.close(is);
			}
		}
	}
}
